package com.goods.controller.business;

import com.goods.business.service.ProductSupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

@Api(tags = "物资来源")
@RestController
@RequestMapping("/business/supplier")
public class ProductSupplierController {

    @Autowired
    private ProductSupplierService productSupplierService;

    @GetMapping("/findSupplierList")
    @ApiOperation("模糊查询物质来源")
    public ResponseBean findSupplierList(Integer pageNum, Integer pageSize, Supplier supplier){
        //
        List<Supplier> supplierList =productSupplierService.findSupplierList(pageNum,pageSize,supplier);
         supplierList = ListPageUtils.page(supplierList, pageSize, pageNum);
        if (!CollectionUtils.isEmpty(supplierList)){
            PageVO<Supplier> pageVO = new PageVO<>(supplierList.size(),supplierList);
            return ResponseBean.success(pageVO);
        }
        return ResponseBean.error("失败");
    }

    @PostMapping("/add")
    @ApiOperation("添加来源")
    public ResponseBean add(@RequestBody Supplier supplier){
        //
        productSupplierService.add(supplier);
        return ResponseBean.success();
    }


    @GetMapping("/edit/{id}")
    @ApiOperation("回显页面")
    public ResponseBean edit(@PathVariable Long id){
        //
       Supplier supplier  =  productSupplierService.edit(id);

        return ResponseBean.success(supplier);
    }


    @PutMapping("/update/{id}")
    @ApiOperation("修改来源信息")
    public ResponseBean update(@PathVariable Long id,@RequestBody Supplier supplier){
        //
        productSupplierService.update(id,supplier);
       return ResponseBean.success();
    }
}
